package com.example.food.controller;

import com.example.food.bean.Hotel;
import com.example.food.bean.User;
import com.example.food.repository.HotelRepository;
import com.example.food.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.jws.soap.SOAPBinding;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private HotelRepository hotelRepository;

    //查询所有用户信息返回列表页面
    @GetMapping("/users")
    public String findUsers(Model model) {
        //获取所以用户
        List<User> users = userRepository.findAll();
        //放入请求域中
        model.addAttribute("hs", users);
        return "user/table_users";
    }

    //所有用户根据用户名查询
    @GetMapping("/user")
    public String findUser(@RequestParam("userName") String userName,
                           Model model) {
        //获取用户
        User user = userRepository.findByUserName(userName);
        //将其放入请求域中
        model.addAttribute("hs", user);
        return "user/table_users";
    }


    //跳转到餐馆添加页面
    @GetMapping("/inAdd")
    public String inAd() {
        return "user/add";
    }

    //添加餐馆用户
    @PostMapping("/addUser")
    public String add(@RequestParam("userName") String userName,
                      @RequestParam("password") String password,
                      @RequestParam("phone") String phone,
                      @RequestParam("hotelName") String hotelName,
                      @RequestParam("intro") String intro,
                      @RequestParam("address") String address) {
        //将用户信息加入user表
        User user = new User();
        user.setUserName(userName);
        user.setPassword(password);
        user.setPhone(phone);
        user.setType("hotel");
        userRepository.save(user);
        //从user表获得user_id
        Integer user_id = user.getU_id();
        System.out.println(user_id);
        //把user_id加入hotel表,还有其他值也加入hotel表
        Hotel hotel = new Hotel();
        hotel.setUserId(user_id);
        hotel.setHotelName(hotelName);
        hotel.setAddress(address);
        hotel.setIntro(intro);
        hotelRepository.save(hotel);

        return "user/add_user_success";
    }

    //查询餐馆用户
    @GetMapping("/find_hotel")
    public String findH(Model model1, Model mode2) {
        //获取用户
        List<User> users = userRepository.findByType("hotel");
        //将其放入请求域1中
        model1.addAttribute("ush", users);

        //获取餐馆信息
        List<Hotel> hotels = hotelRepository.findAll();
        //将其放入请求域2中
        mode2.addAttribute("hh", hotels);
        return "user/table_hotel_user";
    }

    //根据用户名查询餐馆用户
    @GetMapping("/find_a_hotel")
    public String findHN(@RequestParam("userName") String userName,
                         Model model1, Model mode2) {
        //根据类型查出集合list
        List<User> list = userRepository.findByType("hotel");
        //list集合有没有userName
        boolean present = list.stream().filter(m -> m.getUserName().equals(userName)).findAny().isPresent();
        System.out.println("11111111111111111111111111111111111111111111111111111111" + present);

        if (present) {
            //获取用户
            User user = userRepository.findByUserName(userName);
            //将其放入请求域1中
            model1.addAttribute("ush", user);

            //获取用户id
            Integer uid = user.getU_id();
            //根据用户id获取餐馆信息
            Hotel hotel = hotelRepository.findByUserId(uid);
            //将其放入请求域2中
            mode2.addAttribute("hh", hotel);
            return "user/table_hotel_user";
        } else {
            return "error";
        }
    }


    //跳转到普通用户添加页面
    @GetMapping("/inAddP")
    public String inAdP() {
        return "user/addP";
    }

    //添加普通用户
    @PostMapping("/addPerson")
    public String addP(@RequestParam("userName") String userName,
                       @RequestParam("password") String password,
                       @RequestParam("phone") String phone) {
        //将用户信息加入user表
        User user = new User();
        user.setUserName(userName);
        user.setPassword(password);
        user.setPhone(phone);
        user.setType("common");
        userRepository.save(user);
        return "user/addP_user_success";
    }

    //查询普通用户
    @GetMapping("/find_person")
    public String findP(Model model) {
        //获取用户
        List<User> users = userRepository.findByType("common");
        //将其放入请求域中
        model.addAttribute("usp", users);
        return "user/table_person_user";
    }

    //根据用户名查询普通用户
    @GetMapping("/find_a_person")
    public String findA(@RequestParam("userName") String userName,
                        Model model) {
        //根据类型查出集合list
        List<User> list = userRepository.findByType("common");
        //list集合有没有userName
        boolean present = list.stream().filter(m -> m.getUserName().equals(userName)).findAny().isPresent();
        System.out.println("11111111111111111111111111111111111111111111111111111111" + present);
        if (present) {
            //获取用户
            User user = userRepository.findByUserName(userName);
            //将其放入请求域中
            model.addAttribute("usp", user);
            return "user/table_person_user";
        } else {
            return "error";
        }
    }


}
